<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: Origami::Filter::LZW</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">Origami::Filter::LZW</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/origami/filters/lzw_rb.html">
                origami/filters/lzw.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                Object
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Class representing a filter used to <a href="LZW.html#M000363">encode</a>
and <a href="LZW.html#M000364">decode</a> data with <a
href="LZW.html">LZW</a> compression algorithm.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000364">decode</a>&nbsp;&nbsp;
      <a href="#M000363">encode</a>&nbsp;&nbsp;
      <a href="#M000362">new</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name"><a href="../Filter.html">Filter</a></span>
      </div>
    </div>

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Class <a href="LZW/DecodeParms.html" class="link">Origami::Filter::LZW::DecodeParms</a><br />

    </div>




      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000362" class="method-detail">
        <a name="M000362"></a>

        <div class="method-heading">
          <a href="#M000362" class="method-signature">
          <span class="method-name">new</span><span class="method-args">(parameters = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Creates a <a href="LZW.html#M000362">new</a> <a href="LZW.html">LZW</a> <a
href="../Filter.html">Filter</a>.
</p>
<table>
<tr><td valign="top"><em>parameters</em>:</td><td>A hash of filter options (ignored).

</td></tr>
</table>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000362-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000362-source">
<pre>
    <span class="ruby-comment cmt"># File origami/filters/lzw.rb, line 59</span>
59:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">parameters</span> = {})
60:         <span class="ruby-keyword kw">super</span>(<span class="ruby-constant">DecodeParms</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">parameters</span>))
61:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000364" class="method-detail">
        <a name="M000364"></a>

        <div class="method-heading">
          <a href="#M000364" class="method-signature">
          <span class="method-name">decode</span><span class="method-args">(string)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Decodes given data using <a href="LZW.html">LZW</a> compression method.
</p>
<table>
<tr><td valign="top"><em>stream</em>:</td><td>The data to <a href="LZW.html#M000364">decode</a>.

</td></tr>
</table>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000364-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000364-source">
<pre>
     <span class="ruby-comment cmt"># File origami/filters/lzw.rb, line 116</span>
116:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">decode</span>(<span class="ruby-identifier">string</span>)
117:        
118:         <span class="ruby-identifier">result</span> = <span class="ruby-value str">&quot;&quot;</span>
119:         <span class="ruby-identifier">bstring</span> = <span class="ruby-constant">Utils</span><span class="ruby-operator">::</span><span class="ruby-constant">BitReader</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>)
120:         <span class="ruby-identifier">codesize</span> = <span class="ruby-value">9</span>
121:         <span class="ruby-identifier">table</span> = <span class="ruby-identifier">clear</span>(<span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>)
122:         <span class="ruby-identifier">prevbyte</span> = <span class="ruby-keyword kw">nil</span>
123: 
124:         <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">bstring</span>.<span class="ruby-identifier">eod?</span> <span class="ruby-keyword kw">do</span>
125:           <span class="ruby-identifier">byte</span> = <span class="ruby-identifier">bstring</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">codesize</span>)
126: 
127:           <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">table</span>.<span class="ruby-identifier">size</span>
128:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">510</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">10</span>
129:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">1022</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">11</span>
130:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">2046</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">12</span>
131:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">4095</span>
132:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">byte</span> <span class="ruby-operator">!=</span> <span class="ruby-constant">CLEARTABLE</span>
133:               <span class="ruby-keyword kw">then</span>
134:                 <span class="ruby-identifier">raise</span> <span class="ruby-constant">InvalidLZWDataError</span>, 
135:                   <span class="ruby-node">&quot;LZW table is full and no clear flag was set (codeword #{byte.to_s(2).rjust(codesize,'0')} at bit #{bstring.pos - codesize}/#{bstring.size})&quot;</span>
136:               <span class="ruby-keyword kw">end</span>
137:           <span class="ruby-keyword kw">end</span>
138: 
139:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">byte</span> <span class="ruby-operator">==</span> <span class="ruby-constant">CLEARTABLE</span>
140:             <span class="ruby-identifier">codesize</span> = <span class="ruby-value">9</span>
141:             <span class="ruby-identifier">code</span> = <span class="ruby-constant">EOD</span>
142:             <span class="ruby-identifier">clear</span> <span class="ruby-identifier">table</span>
143:             <span class="ruby-identifier">prevbyte</span> = <span class="ruby-keyword kw">nil</span>
144:             <span class="ruby-keyword kw">redo</span>
145:           <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">byte</span> <span class="ruby-operator">==</span> <span class="ruby-constant">EOD</span>
146:             <span class="ruby-keyword kw">break</span>
147:           <span class="ruby-keyword kw">else</span>
148:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">prevbyte</span>.<span class="ruby-identifier">nil?</span>
149:               <span class="ruby-identifier">prevbyte</span> = <span class="ruby-identifier">byte</span>
150:               <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">table</span>.<span class="ruby-identifier">key</span>(<span class="ruby-identifier">byte</span>)
151:               <span class="ruby-keyword kw">redo</span>
152:             <span class="ruby-keyword kw">else</span>
153:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">table</span>.<span class="ruby-identifier">has_value?</span>(<span class="ruby-identifier">byte</span>)
154:                 <span class="ruby-identifier">entry</span> = <span class="ruby-identifier">table</span>.<span class="ruby-identifier">key</span>(<span class="ruby-identifier">byte</span>)
155:               <span class="ruby-keyword kw">else</span>
156:                 <span class="ruby-identifier">entry</span> = <span class="ruby-identifier">table</span>.<span class="ruby-identifier">key</span>(<span class="ruby-identifier">prevbyte</span>)
157:                 <span class="ruby-identifier">entry</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">entry</span>[<span class="ruby-value">0</span>,<span class="ruby-value">1</span>]
158:               <span class="ruby-keyword kw">end</span>
159: 
160:               <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">entry</span>
161:               <span class="ruby-identifier">table</span>[<span class="ruby-identifier">table</span>.<span class="ruby-identifier">key</span>(<span class="ruby-identifier">prevbyte</span>) <span class="ruby-operator">+</span> <span class="ruby-identifier">entry</span>[<span class="ruby-value">0</span>,<span class="ruby-value">1</span>]] = <span class="ruby-identifier">table</span>.<span class="ruby-identifier">size</span>
162:               <span class="ruby-identifier">prevbyte</span> = <span class="ruby-identifier">byte</span>
163:             <span class="ruby-keyword kw">end</span>
164:           <span class="ruby-keyword kw">end</span>
165:         <span class="ruby-keyword kw">end</span>
166:  
167:         <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Predictor</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>)
168:           <span class="ruby-identifier">colors</span>  = <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Colors</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>) <span class="ruby-operator">?</span>  <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Colors</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">1</span>
169:           <span class="ruby-identifier">bpc</span>     = <span class="ruby-ivar">@params</span>.<span class="ruby-constant">BitsPerComponent</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">BitsPerComponent</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">8</span>
170:           <span class="ruby-identifier">columns</span> = <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Columns</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Columns</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">1</span>
171: 
172:           <span class="ruby-identifier">result</span> = <span class="ruby-constant">Predictor</span>.<span class="ruby-identifier">do_post_prediction</span>(<span class="ruby-identifier">result</span>, <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Predictor</span>.<span class="ruby-identifier">to_i</span>, <span class="ruby-identifier">colors</span>, <span class="ruby-identifier">bpc</span>, <span class="ruby-identifier">columns</span>)
173:         <span class="ruby-keyword kw">end</span>
174: 
175:         <span class="ruby-identifier">result</span>
176:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000363" class="method-detail">
        <a name="M000363"></a>

        <div class="method-heading">
          <a href="#M000363" class="method-signature">
          <span class="method-name">encode</span><span class="method-args">(string)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Encodes given data using <a href="LZW.html">LZW</a> compression method.
</p>
<table>
<tr><td valign="top"><em>stream</em>:</td><td>The data to <a href="LZW.html#M000363">encode</a>.

</td></tr>
</table>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000363-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000363-source">
<pre>
     <span class="ruby-comment cmt"># File origami/filters/lzw.rb, line 67</span>
 67:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">encode</span>(<span class="ruby-identifier">string</span>)
 68:         <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Predictor</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>)
 69:           <span class="ruby-identifier">colors</span>  = <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Colors</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>) <span class="ruby-operator">?</span>  <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Colors</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">1</span>
 70:           <span class="ruby-identifier">bpc</span>     = <span class="ruby-ivar">@params</span>.<span class="ruby-constant">BitsPerComponent</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">BitsPerComponent</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">8</span>
 71:           <span class="ruby-identifier">columns</span> = <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Columns</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Columns</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">1</span>
 72: 
 73:           <span class="ruby-identifier">string</span> = <span class="ruby-constant">Predictor</span>.<span class="ruby-identifier">do_pre_prediction</span>(<span class="ruby-identifier">string</span>, <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Predictor</span>.<span class="ruby-identifier">to_i</span>, <span class="ruby-identifier">colors</span>, <span class="ruby-identifier">bpc</span>, <span class="ruby-identifier">columns</span>)
 74:         <span class="ruby-keyword kw">end</span>       
 75:         
 76:         <span class="ruby-identifier">codesize</span> = <span class="ruby-value">9</span>
 77:         <span class="ruby-identifier">result</span> = <span class="ruby-constant">Utils</span><span class="ruby-operator">::</span><span class="ruby-constant">BitWriter</span>.<span class="ruby-identifier">new</span>
 78:         <span class="ruby-identifier">result</span>.<span class="ruby-identifier">write</span>(<span class="ruby-constant">CLEARTABLE</span>, <span class="ruby-identifier">codesize</span>)
 79:         <span class="ruby-identifier">table</span> = <span class="ruby-identifier">clear</span>({})
 80:         
 81:         <span class="ruby-identifier">s</span> = <span class="ruby-value str">''</span>        
 82:         <span class="ruby-identifier">string</span>.<span class="ruby-identifier">each_byte</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">byte</span><span class="ruby-operator">|</span>
 83:           <span class="ruby-identifier">char</span> = <span class="ruby-identifier">byte</span>.<span class="ruby-identifier">chr</span>
 84:           
 85:           <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">table</span>.<span class="ruby-identifier">size</span>
 86:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">512</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">10</span>
 87:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">1024</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">11</span>
 88:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">2048</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">12</span>
 89:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">4096</span>
 90:               <span class="ruby-identifier">result</span>.<span class="ruby-identifier">write</span>(<span class="ruby-constant">CLEARTABLE</span>, <span class="ruby-identifier">codesize</span>)
 91:               <span class="ruby-identifier">codesize</span> = <span class="ruby-value">9</span>
 92:               <span class="ruby-identifier">clear</span> <span class="ruby-identifier">table</span>
 93:               <span class="ruby-keyword kw">redo</span>
 94:           <span class="ruby-keyword kw">end</span>
 95:          
 96:           <span class="ruby-identifier">it</span> = <span class="ruby-identifier">s</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">char</span>
 97:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">it</span>)
 98:             <span class="ruby-identifier">s</span> = <span class="ruby-identifier">it</span>
 99:           <span class="ruby-keyword kw">else</span>
100:             <span class="ruby-identifier">result</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">table</span>[<span class="ruby-identifier">s</span>], <span class="ruby-identifier">codesize</span>)
101:             <span class="ruby-identifier">table</span>[<span class="ruby-identifier">it</span>] = <span class="ruby-identifier">table</span>.<span class="ruby-identifier">size</span>
102:             <span class="ruby-identifier">s</span> = <span class="ruby-identifier">char</span>
103:           <span class="ruby-keyword kw">end</span>
104:         <span class="ruby-keyword kw">end</span>
105:          
106:         <span class="ruby-identifier">result</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">table</span>[<span class="ruby-identifier">s</span>], <span class="ruby-identifier">codesize</span>)
107:         <span class="ruby-identifier">result</span>.<span class="ruby-identifier">write</span>(<span class="ruby-constant">EOD</span>, <span class="ruby-identifier">codesize</span>)
108:         
109:         <span class="ruby-identifier">result</span>.<span class="ruby-identifier">final</span>.<span class="ruby-identifier">to_s</span>
110:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>